package presentation.panels;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;

import metier.entite.user.TypeUser;
import metier.entite.user.User;
import metier.sercurity.PasswordEncryptor;
import persistance.userDB.UserDB;
import presentation.composents.TempsLabel;



@SuppressWarnings("serial")
public class PanelAjoutUsr extends JPanel {

	private JLabel jLabel1;
	private JLabel jLabel2;
	private JLabel jLabel3;
	private JRadioButton jRadioButton1;
	private JRadioButton jRadioButton2;
	private JTextField jTextField1;
	private JPasswordField jTextField2;
	private JButton create ;
	private JFrame parent ;
	private TempsLabel endQueryLabel ;
	private JPasswordField passwordField2 ;
	private JLabel labelConfirmPassword ;

	public PanelAjoutUsr (JFrame parent){
		super() ;
		this.parent = parent ;
		initComponents();
		initJRadios() ;

	}

	private  TypeUser selected ;


	private void initComponents() {

		jLabel1 = new JLabel();
		jLabel2 = new JLabel();
		jLabel3 = new JLabel();
		jTextField1 = new JTextField();
		jTextField2 = new JPasswordField();
		jRadioButton1 = new JRadioButton();
		jRadioButton2 = new JRadioButton();
		endQueryLabel = new TempsLabel("") ;
		labelConfirmPassword = new JLabel("Confirm password") ;
		passwordField2 = new JPasswordField() ;

		create = new JButton("Ok") ;

		jLabel1.setText("Utilisateur");

		jLabel2.setText("Password");

		jLabel3.setText("Droit d'access");

		jRadioButton1.setText("Client");

		jRadioButton2.setText("Admin");

		GroupLayout layout = new GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addGap(59, 59, 59)
						.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
								.addComponent(jLabel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(jLabel2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(labelConfirmPassword, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(jLabel3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
								.addGap(18, 18, 18)
								.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
										.addComponent(jRadioButton2)
										.addComponent(create)
										.addComponent(endQueryLabel)
										.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
												.addComponent(jTextField1, GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE)
												.addComponent(jTextField2)
												.addComponent(passwordField2))
												.addComponent(jRadioButton1))
												.addContainerGap(88, Short.MAX_VALUE))
				);
		layout.setVerticalGroup(
				layout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addGap(52, 52, 52)
						.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING,false)
								.addComponent(jLabel1)
								.addComponent(jTextField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
								.addGap(18, 18, 18)
								.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,false)
										.addComponent(jLabel2)
										.addComponent(jTextField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
										.addGap(18, 18, 18)
										.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,false)
										.addComponent(labelConfirmPassword)
										.addComponent(passwordField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
										.addGap(18, 18, 18)
										.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel3)
												.addComponent(jRadioButton1))
												.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
												.addComponent(jRadioButton2)
												.addComponent(create)
												.addComponent(endQueryLabel)
												.addContainerGap(123, Short.MAX_VALUE))
				);
	}     

	private void initJRadios(){
		jRadioButton1.setSelected(true) ;
		selected = TypeUser.CLIENT ;

		jRadioButton1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (!selected.equals(TypeUser.CLIENT)){
					selected = TypeUser.CLIENT ;
					jRadioButton2.setSelected(false) ;
				} else {
					jRadioButton1.setSelected(true) ;
				}

			}
		}) ;
		jRadioButton2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (!selected.equals(TypeUser.ADMIN)){
					selected = TypeUser.ADMIN ;
					jRadioButton1.setSelected(false) ;
				} else {
					jRadioButton2.setSelected(true) ;
				}

			}
		}) ;

		create.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (jTextField1.getText() == null || jTextField1.getText().isEmpty() 
						|| jTextField2.getPassword() == null || passwordField2.getPassword() == null ){
					jTextField2.setText("") ;
					setEndQueryLabel(false,"Veuillez saisir toutes les informations") ;
					return ;
				}
				
				if (getEncryptedPassword() == null ){
					setEndQueryLabel(false,"Veuillez saisir des mots de passes identiques") ;
					return ;
				}
				
				User user = new User(jTextField1.getText(),getEncryptedPassword(),selected) ;
				
				try {
					if (UserDB.isUserExistant(user)){
						setEndQueryLabel(false,"Utilisateur existant") ;
					} else {
						UserDB.addUser(user) ;
						setEndQueryLabel(true,"User added") ;
					}
				} catch (ClassNotFoundException e1) {
					e1.printStackTrace();
				} catch (SQLException e1) {
					e1.printStackTrace();
					setEndQueryLabel(false,"User not added") ;
					JOptionPane.showMessageDialog(parent, "Data Base Error");
				}
			}
		}) ;

	}

	private void endQuery() {
		jTextField2.setText(null) ;
		jTextField1.setText(null) ;
		passwordField2.setText(null) ;
		jRadioButton1.setSelected(true) ;
		selected = TypeUser.CLIENT ;		
	}
	
	protected void setEndQueryLabel(boolean isValid , String text){
		if (isValid){
			endQuery() ;
			endQueryLabel.setText(text) ;
			endQueryLabel.setForeground(Color.green);
			endQueryLabel.setValide(true) ;
		} else {
			endQueryLabel.setText(text) ;
			endQueryLabel.setForeground(Color.red);
			endQueryLabel.setValide(true) ;
			passwordField2.setText(null) ;
			jTextField2.setText(null) ;
		}
	}

	protected String getEncryptedPassword(){
		String password1 = convertPasswordToString(jTextField2.getPassword()) ;
		String password2 = convertPasswordToString(passwordField2.getPassword()) ;
		if (password1.isEmpty() || password2.isEmpty()){
			return null ;
		}
		if (password1.equals(password2)){
			System.out.println("[DEBUG] -- Panel Ajouter");
			System.out.println(password1);
			return PasswordEncryptor.encode(password1) ;
		}
		return null ;
	}
	
	private String convertPasswordToString(char[] pass){
		StringBuilder password = new StringBuilder("") ;
		for (int i =0; i<pass.length ; i++ ){
			password.append(pass[i]) ;
		}
		return password.toString() ;
	}

}
